package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;

import java.net.URLEncoder;

/**
 * @description: 打卡作业详情页面
 * @author: wyk
 * @time: 2021/10/12 9:40
 */
public class DKWorkDetailsActivity extends BaseHtmlActicity {
    private static final String TAG = "DKWorkDetailsActivity";

    private final int state = 3;
    /**
     * workId：作业id
     * className: 班级名
     */
    private String workId,className,clientId,brandId,startUrl,accessToken;

    @Override
    protected void preCreate() {
        super.preCreate();
    }

    @Override
    protected void init() {
        super.init();
        setTitleText(R.string.publish_dk_work);
        Intent intent = getIntent();
        workId = intent.getStringExtra(IntentFlag.INTENT_FLAG_WORK_ID);
        className = intent.getStringExtra(IntentFlag.INTENT_FLAG_CLASS_NAME);
        className = toUtf8(className);
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            clientId = verifyInfo.getClientId();
            brandId = EApplication.BRAND_ID + "";
            accessToken = verifyInfo.getAccessToken();
            startUrl = EpaperConstant.URL_H5_JUMP_TO_DK_DETAILS + "workId=" + workId +
                    "&className=" + className+ "&clientId=" + clientId + "&brandId="+brandId +
                    "&state=" + 3 + "&wappid=" + "" + "&accessToken=" + accessToken;
            setStartURL(startUrl);
            loadUrl(startUrl);
        }else {
            showToastError(getString(R.string.get_user_info_error));
            finish();
        }

    }

    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str,"UTF-8");
            result = result.replace("+", "%20");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }

    public void onEventMainThread(SubmitEvent submitEvent) {
        CWLog.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("DKWorkDetailsActivity")) {//
            /*JumpServiceParamBean jumpServiceParaBean = gson.fromJson(submitEvent.getJson(), JumpServiceParamBean.class);
            int serviceId = jumpServiceParaBean.getServiceId();
            String urlH5 = jumpServiceParaBean.getUrl();*/
            jumpToInnerResouce(submitEvent.getJson());
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);
    }
}
